package com.meihua.shellapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.bean.AreaBean;
import com.meihua.shellapp.databean.AreaDataBean;
import com.meihua.shellapp.utils.ConstantUtils;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import java.util.ArrayList;
import java.util.List;

/**
 * 三级联动
 * 作者：lc on 2017/10/14.
 * 邮箱：lc0607live@163.com
 */

public abstract class LinkageDialog extends Dialog implements View.OnClickListener {

    private LinearLayout ll_dialog;//布局总id

    private NumberPicker np_one;//省
    private NumberPicker np_two;//市
    private NumberPicker np_three;//区
    private TextView tv_cencle;//取消
    private TextView tv_sure;//确定

    private List<AreaDataBean> data = new ArrayList<>();

    public LinkageDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.gravity = Gravity.BOTTOM;
        lp.width = ConstantUtils.getInstance().getScreenW(context);
        getWindow().setAttributes(lp);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_linkage);
        initView();
        data = SharedPreferencesUtil.getInstance().getObject(getContext(), "area", data.getClass());
        if (data == null || data.size() == 0) {
            getAreaData();
        } else {
            bindData();
        }
    }

    /**
     * 加载布局
     */
    private void initView() {
        np_one = findViewById(R.id.np_one);
        np_two = findViewById(R.id.np_two);
        np_three = findViewById(R.id.np_three);
        tv_cencle = findViewById(R.id.tv_cencle);
        tv_sure = findViewById(R.id.tv_sure);
        ll_dialog = findViewById(R.id.ll_dialog);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ll_dialog.getLayoutParams();
        lp.width = ConstantUtils.getInstance().getScreenW(getContext());
        ll_dialog.setLayoutParams(lp);
        tv_sure.setOnClickListener(this);
        tv_cencle.setOnClickListener(this);
//        np_one.setEnabled(false);
    }

    /**
     * 绑定省级数据
     */
    private void bindData() {
        String[] temp = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {
            temp[i] = data.get(i).getName();
        }
        np_one.setDisplayedValues(null);
        np_one.setMinValue(0);
        np_one.setMaxValue(temp.length - 1);
        np_one.setDisplayedValues(temp);
        bindCityData(data.get(0).getCity());
        bindCounty(data.get(0).getCity().get(0).getTown());
        np_one.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                bindCityData(data.get(i1).getCity());
                bindCounty(data.get(i1).getCity().get(0).getTown());
            }
        });
    }

    /**
     * 绑定市级数据
     *
     * @param citys
     */
    private void bindCityData(final List<AreaDataBean> citys) {
        String[] temp = new String[citys.size()];
        for (int i = 0; i < citys.size(); i++) {
            temp[i] = citys.get(i).getName();
        }
        np_two.setDisplayedValues(null);
        np_two.setMinValue(0);
        np_two.setMaxValue(temp.length - 1);
        np_two.setDisplayedValues(temp);
        np_two.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                bindCounty(citys.get(i1).getTown());
            }
        });
    }

    /**
     * 绑定区级数据
     *
     * @param county
     */
    private void bindCounty(final List<AreaDataBean> county) {
        String[] temp = new String[county.size()];
        for (int i = 0; i < county.size(); i++) {
            temp[i] = county.get(i).getName();
        }
        np_three.setDisplayedValues(null);
        np_three.setMinValue(0);
        np_three.setMaxValue(county.size() - 1);
        np_three.setDisplayedValues(temp);
        np_three.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
//                bindCityData(county.get(i).getTown());
            }
        });
    }

    /**
     * 获取数据
     */
    private void getAreaData() {
        OkGo.<String>post(URL.getUrl().GETAREA)
                .execute(new MyStringCallBack<AreaBean>(null) {
                    @Override
                    public void onSuccessData(AreaBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            data = bean.getData();
                            bindData();
                            SharedPreferencesUtil.getInstance().setObject(getContext(), "area", data);
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cencle://取消
                dismiss();
                break;
            case R.id.tv_sure://确定
                getArea(data.get(np_one.getValue()),
                        data.get(np_one.getValue()).getCity().get(np_two.getValue()),
                        data.get(np_one.getValue()).getCity().get(np_two.getValue()).getTown().get(np_three.getValue()));
                dismiss();
                break;
        }
    }

    public abstract void getArea(AreaDataBean province, AreaDataBean city, AreaDataBean county);
}
